package org.supper.auth.exception;

import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;

import java.io.IOException;

public class DefaultErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String s, Response response) {
        Exception exception = null;
        String bodyStr = null;
        try {
            bodyStr = Util.toString(response.body().asReader());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //除了200的其余异常走FeignBadRequestException
        if (response.status() != 200) {
            return new FeignBadRequestException(response.status(), bodyStr);
        }

        return new RuntimeException(bodyStr);
    }
}